# -*- coding: utf-8 -*-
import scrapy,getpass


class RrloginSpider(scrapy.Spider):
    name = 'rrlogin'
    allowed_domains = ['http://www.renren.com/']
    # start_urls = ['http://http://www.renren.com//']

    def start_requests(self):
        url = 'http://www.renren.com/PLogin.do'
        email = input("请输入人人网账号:")
        password = getpass.getpass("请输入人人网密码:")
        data = {'email': email,
                'password': password,}
        # FormRequest 是Scrapy发送POST请求的方法
        yield scrapy.FormRequest(
            url=url,
            formdata=data,
            callback=self.parse
        )

    def parse(self, response):
        result = response.text
        # 通过关键字判断是否登录成功
        if '新鲜事' in result:
            print('登录成功')
            print("进入个人主页")
            print("获取个人主页菜单")
            menulist = response.selector.css("div.app-nav-wrap div.app-nav-cont ul li")
            count = 0
            for menu in menulist:
                count += 1
                print(count, ".", menu.css("span::text").extract_first())
            print("访问结束")
        else:
            # 获取登录失败信息
            error_info = response.selector.css("#yellow_error p.wrong::text").extract_first()
            print('登录失败:', error_info)
            # print(response.text)

